package com.nulldev.util.web.nTubeExtensions.fuckAdvertising;

import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.JVM.JVM;
import com.nulldev.util.VariableAPI.ArrayUtils;
import com.nulldev.util.concurrency.threadIt.v4.emapi.ExecutorManager;
import com.nulldev.util.concurrency.threadIt.v4.emapi.uft.Executable;
import com.nulldev.util.data.Arrays.maps.FastMaps;
import com.nulldev.util.io.FastIO.FastIO;
import com.nulldev.util.web.YouTubeExtractor.YouTubeExtractor;
import com.nulldev.util.web.YouTubeExtractor.extractors.IYouTubeExtractor;
import com.nulldev.util.web.YouTubeExtractor.extractors.IYouTubeExtractorConstants;
import com.nulldev.util.web.YouTubeExtractor.extractors.util.YTIAPIClientType;
import com.nulldev.util.web.YouTubeExtractor.video.ExtractedVideo;

public class TotalAdvertiserDeathDemo {

	public static void main(String[] args) {
		Arguments.setup(args);
		final IYouTubeExtractor ex = YouTubeExtractor.get("YouTubeExtractor.NFEE_rolling_release");
		ex.enableInstanceDebugging(true);
		final String vID = "JkChW6WP0hU";
		final Executable<ExtractedVideo> f = ex.generateTask(FastMaps.fixedUniMap(new Object[][]
			{
					{ IYouTubeExtractorConstants.MAP_TYPE_VIDEO_ID, vID },
					{ IYouTubeExtractorConstants.MAP_TYPE_ENABLE_DEBUG_API_RESPONSE_PRINT, true },
					{ IYouTubeExtractorConstants.MAP_TYPE_LIVESTREAMS_KEEP_RAW_STREAMS, true },
					{ IYouTubeExtractorConstants.MAP_TYPE_ENABLE_ADVERTISING_METADATA_EXTRACTION, true },
					{ IYouTubeExtractorConstants.MAP_TYPE_USE_CLIENT_TYPE, YTIAPIClientType.ANDROID } }));
		ExecutorManager.get().asyncSubmit(f);
		final long s0 = System.currentTimeMillis();
		JVM.println("result: " + f.waitTillCompletion());
		JVM.println("sync: " + f);
		final long e0 = System.currentTimeMillis();
		JVM.println("Execution Time: " + (e0 - s0) + " ms");
		if (f.result() == null) {
			throw new NullPointerException("Failed to get a result!");
		}
		if (f.result().supportsLateExtraction()) {
			f.result().awaitCompletion();
			JVM.println("lateExtraction: COMPLETE");
		}
		if (!f.result().getResult().successful()) {
			JVM.println("Failed to extract.");
			FastIO.get().flush();
			JVM.exit();
			return;
		}
		if (f.result().hasAdvertisingMetadata()) {
			JVM.println("advertising: ");
			ArrayUtils.printLln(f.result().getAdvertisingMetadata().getAdPlacements());
		}
		JVM.println("Invoking TAD(TM)...");
		f.result().getAdvertisingMetadata().getAdPlacements().forEach((placement) -> {
			try {
				TotalAdvertiserDeath.invokeAdViewOrWatch(placement, vID);
			} catch (Exception ex1) {
				JVM.errorln(ex1);
			}
		});
		FastIO.get().flush();
		JVM.exit();
	}
}
